package com.zhq.wx.filter;

import com.zhq.bean.User;
import com.zhq.util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author:钟豪强
 * @Description:
 * @Date Created in  2020-08-${Day} 0:07
 * @Modified By:
 */
@WebFilter("/index.html")
public class UserFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpSession session = ((HttpServletRequest)req).getSession();
        HttpServletResponse response = (HttpServletResponse) resp;
        User wxUser = UserUtil.getWxUser(session);
        if (wxUser != null) {
            //用户登录了,放行
            chain.doFilter(req, resp);

        }else{
            response.sendRedirect("login.html");
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
